package yxy.game.pm2.module.stairway;

import org.joda.time.DateTime;
import org.redisson.api.RList;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.battle.module.record.result.BattleResult;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.db.newDao.StairwayRecordDAO;
import yxy.game.pm2.bean.db.po.StairwayRecordPo;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.ArrayList;
import java.util.List;

public class StairwayRecord extends PlayerModule {
    // bean===========================================================================
    private DateTime date;// 时间
    private String recordCode;// 录像code

    private boolean isAttacker;// 玩家是进攻者

    private int rank;// 排名变化

    private String name;// 对手名字
    private int head;// 对手头像
    private int headframe;// 对手头像框
    private int level;// 对手等级
    private long power;// 对手战力

    private BattleResult battleResult;


    public DateTime getDate() {
        return date;
    }

    public void setDate(DateTime date) {
        this.date = date;
    }

    public String getRecordCode() {
        return recordCode;
    }

    public void setRecordCode(String recordCode) {
        this.recordCode = recordCode;
    }

    public boolean isAttacker() {
        return isAttacker;
    }

    public void setAttacker(boolean isAttacker) {
        this.isAttacker = isAttacker;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHead() {
        return head;
    }

    public void setHead(int head) {
        this.head = head;
    }

    public int getHeadframe() {
        return headframe;
    }

    public void setHeadframe(int headframe) {
        this.headframe = headframe;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public long getPower() {
        return power;
    }

    public void setPower(long power) {
        this.power = power;
    }

    public BattleResult getBattleResult() {
        return battleResult;
    }

    public void setBattleResult(BattleResult battleResult) {
        this.battleResult = battleResult;
    }

    // func===========================================================================
    // redis==========================================================================

    static public String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:STAIRWAYRECORD", playerCode);
    }

    static private RList<StairwayRecord> touch(String playerCode) throws RedisException {
        return RedisManager.client(redis()).getList(key(playerCode));
    }

    static public List<StairwayRecord> get(String playerCode) {
        try {
            List<StairwayRecord> list = touch(playerCode).readAll();
            //缓存没数据从数据库读取并放到redis
            if (list.size() <= 0) {
                List<StairwayRecordPo> poList = StairwayRecordDAO.getList(playerCode);
                for (int i = 0; i < poList.size(); i++) {
                    StairwayRecord stairwayRecord = toBean(poList.get(i));
                    list.add(stairwayRecord);
                }

                if (list.size() > 0) {
                    //将数据放到缓存
                    touch(playerCode).addAll(list);
                }
            }
            return list;
        } catch (RedisException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    static public void add(String playerCode, StairwayRecord record) {
        try {
            RList<StairwayRecord> rlist = touch(playerCode);
            rlist.add(0, record);
            if (rlist.size() > 10) {
                rlist.trim(0, 9);
            }
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    public static void clear(String playerCode) {
        try {
            touch(playerCode).clear();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            RList<StairwayRecord> rList = touch(playerCode);

            if (rList.isExists()) {
                long updateTime = System.currentTimeMillis();
                //存储
                List<StairwayRecordPo> StairwayRecordPoList = new ArrayList<>();
                List<StairwayRecord> StairwayRecordList = rList.readAll();
                for (StairwayRecord record : StairwayRecordList) {
                    StairwayRecordPo po = toDbBean(record, playerCode, updateTime);
                    StairwayRecordPoList.add(po);
                }

                //保存数据库
                StairwayRecordDAO.replace(StairwayRecordPoList);
                //
                StairwayRecordDAO.delete(playerCode, updateTime);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //将bean转成po
    private static StairwayRecordPo toDbBean(StairwayRecord record, String playerCode, long updateTime) {
        StairwayRecordPo stairwayRecordPo = new StairwayRecordPo();
        stairwayRecordPo.setPlayerCode(playerCode);
        stairwayRecordPo.setDate(record.getDate().toDate());
        stairwayRecordPo.setRecordCode(record.getRecordCode());
        stairwayRecordPo.setAttacker(record.isAttacker());
        stairwayRecordPo.setRank(record.getRank());
        stairwayRecordPo.setHead(record.getHead());
        stairwayRecordPo.setName(record.getName());
        stairwayRecordPo.setHeadframe(record.getHeadframe());
        stairwayRecordPo.setLevel(record.getLevel());
        stairwayRecordPo.setPower(record.getPower());
        stairwayRecordPo.setBattleResult(record.getBattleResult().toString());
        stairwayRecordPo.setUpdateTime(updateTime);
        return stairwayRecordPo;
    }


    //将po转成bean
    private static StairwayRecord toBean(StairwayRecordPo po) {
        StairwayRecord stairwayRecord = new StairwayRecord();
        stairwayRecord.setPlayerCode(po.getPlayerCode());
        stairwayRecord.setDate(new DateTime(po.getDate()));
        stairwayRecord.setRecordCode(po.getRecordCode());
        stairwayRecord.setAttacker(po.isAttacker());
        stairwayRecord.setRank(po.getRank());
        stairwayRecord.setName(po.getName());
        stairwayRecord.setHead(po.getHead());
        stairwayRecord.setHeadframe(po.getHeadframe());
        stairwayRecord.setLevel(po.getLevel());
        stairwayRecord.setPower(po.getPower());
        stairwayRecord.setBattleResult(BattleResult.battleResult(po.getBattleResult()));
        return stairwayRecord;
    }

    //移除redis
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }
    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

}
